<?php
namespace Api\Controller;
use Common\Controller\Base;
use Think\Log;

/**
 * author: Administrator
 * Createtime: 2015/10/278:58
 * description: app移动接口
 */
class AppBaseController extends Base
{
    public $appid;
    public $appsecret;

    //会员模型相关配置
    protected $memberConfig = array();

    //用户信息
    protected $userInfo = array();
    //用户管理表信息
    protected $memberInfo = array();
    //会员数据库对象
    protected $memberDb = NULL;
    //模型id
    protected $modelid = null;

    //初始化
    protected function _initialize()
    {
        parent::_initialize();

        //特定方法无需验证Token
        if ( !in_array(ACTION_NAME, array('test', 'ces','message','getToken','getAppid','validateToken'))) {
           // $this->validateToken();
        }

    }

    /**
     *覆盖父类方法
     */
    public function cityId()
    {

    }
    /**
     * 信息提示
     * @access protected
     * @param string $message 错误信息，可以是错误代码
     * @param array $data 附带数据
     * @param $type error 错误提示，success，普通小心提示
     * @return void
     */
    protected function message($message = '', $data = array(), $type = 'success')
    {
        //如果是错误代码
        if (is_numeric($message)) {
            switch ($message) {
                case 1:
                    $message = '名字中带有禁用词，请更换一个！';
                    $data['error'] = 1;
                    break;
                case 2:
                    $message = '名字不能超过12个字母或6个汉字！';
                    $data['error'] = 2;
                    break;
                case 3:
                    $message = '名字中至少要含有一个字母或汉字！';
                    $data['error'] = 3;
                    break;
                case 4:
                    $message = '输入的电子邮箱格式不正确！';
                    $data['error'] = 4;
                    break;
                case 10000:
                    $message = '操作成功！';
                    $data['error'] = 10000;
                    break;
                case 10005:
                    $message = '登陆账号不能为空！';
                    $data['error'] = 10005;
                    break;
                case 10006:
                    $message = '分组名超过不能超过七个字！';
                    $data['error'] = 10006;
                    break;
                case 10007:
                    $message = '分组名称不能为空！';
                    $data['error'] = 10007;
                    break;
                case 1011:
                    $message = '该帐号已经存在，请更换一个！';
                    $data['error'] = 1011;
                    break;
                case 20001:
                    $message = '您没有登录或已经退出，请登录后再进行操作 ！';
                    $data['error'] = 20001;
                    break;
                case 20002:
                    $message = '您没有权限进行操作 ！';
                    $data['error'] = 20002;
                    break;
                case 20011:
                    $message = '此邮箱已经存在，请更换一个！';
                    $data['error'] = 20011;
                    break;
                case 20014:
                    $message = '您的账号需要管理员审核后才能登陆！';
                    $data['error'] = 20014;
                    break;
                case 20015:
                    $message = '邮箱账号未激活！';
                    $data['error'] = 20015;
                    break;
                case 20021:
                    $message = '两次输入密码不相同！';
                    $data['error'] = 20021;
                    break;
                case 20022:
                    $message = '当前密码不正确，请从新输入！';
                    $data['error'] = 20022;
                    break;
                case 20023:
                    $message = '账号或密码错误！';
                    $data['error'] = 20023;
                    break;
                case 20024:
                    $message = '请输入您的密码！';
                    $data['error'] = 20024;
                    break;
                case 20025:
                    $message = '密码长度应是6位以上！';
                    $data['error'] = 20025;
                    break;
                case 20031:
                    $message = '验证码错误！';
                    $data['error'] = 20031;
                    break;
                case 20051:
                    $message = 'appsecret or appid  error！';
                    $data['error'] = 20051;
                    break;
                case 20052:
                    $message = 'invalid token';
                    $data['error'] = 20052;
                    break;
                case 30001:
                    $message = '60秒后才能继续发送短信验证码';
                    $data['error'] = 30001;
                    break;
                case 30002:
                    $messane = '短信验证码错误';
                    $data['error'] = 30002;
                    break;
                default:
                    break;
            }
        }
        //如果是数组
        if (is_array($message)) {
            $info = $message['info'];
            $error = $message['error'];
            $message = $info;
            $data['error'] = $error;
        }

        $callback = I('request.callback', '');
        if (empty($callback)) {
            $type = 'JSON';
        } else {
            $type = 'JSONP';
            C('VAR_JSONP_HANDLER', 'callback');
        }
        $data['info'] = $message;
        $data['status'] = $data['error'] == 10000 ? 1 : 0;
//        dump($data);
        $this->ajaxReturn($data, $type);
        exit;
    }

    /*获取getToken*/
    public function getToken()
    {
        $this->appid = I('request.appid');
        $this->appsecret = I('request.appsecret');
        $row = M('appkey')->where(['appid'=>$this->appid, 'appsecret'=>$this->appsecret])->find();
        /*        if ($appsecret != $this->appsecret || $appid != $this->appid) {
                    $this->message(20051, array(), 'error');
                }*/
        if (!$row) {
            $this->message(20051, array(), 'error');
        }
        $ori_str = S($this->appid . '_' . $this->appsecret);   //这里appid和appsecret我写固定了，实际是通过客户端获取  所以这里我们可以做很多 比如判断appid和appsecret有效性等
        if ($ori_str) {       //重新获取就把以前的token删除
            //S($ori_str,null);
        }

        //这里是token产生的机制  您也可以自己定义
        $nonce = genRandomString(16);
        $tmpArr = array($nonce, $this->appid, $this->appsecret);

        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        //这里做了缓存 'a'=>b 和'b'=>a格式的缓存
        $expires_in = 7200;
        S($this->appid . '_' . $this->appsecret, $tmpStr, $expires_in);
        S($tmpStr, $this->appid . '_' . $this->appsecret, $expires_in);
        cookie('token', $tmpStr);
        $this->message("success", [
            'error' => 10000,
            'expires_in' => $expires_in,
            'access_token' => $tmpStr,
        ]);
    }

    //根据Token获取appid
    public function getAppid()
    {
        if (isset($_REQUEST['access_token'])) {
            $access_token = explode('_',S($_REQUEST['access_token']));
            return $access_token[0];
        }
    }

    /*验证Token*/
    /**
     *
     */
    public function validateToken()
    {
        if (!isset($_REQUEST['access_token'])) {
            $this->message('access_token is null', [], 'error');
        } else if (!S($_REQUEST['access_token'])) {
            Log::write($_REQUEST['access_token'] . ' is error', 'INFO');
            $this->message(20052, [], 'error');
        }
    }

    /*测试*/
    public function test()
    {
        $this->display();
    }
}